<template>
  <div class="main">
    <div class="login">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="290px" class="demo-ruleForm">
        <div class="text">
          欢迎来到智慧医疗
        </div>
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
        <div class="sign">还没有账号？点击<span style="color: blue;cursor: pointer;" @click="$router.push('/signup')">注册</span></div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {userLogin} from '../api/user'
import router from "@/router";
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        password: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          userLogin(this.ruleForm).then(res=>{
            if(res.code==200){
              //登录成功
              alert(res.msg)
              localStorage.setItem("token",res.data.token)
              localStorage.setItem("name",res.data.name)
              this.$router.push("/index")
            }else{
              //登录失败
              alert(res.msg)
            }
          }).catch(error=>{})
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

.login{
  height: 50vh;
  width: 50%;
  display: flex;
  background-color: aliceblue;
  border-radius: 5px;
  box-shadow: 2px 0 6px rgba(0,21,41,.35);
}
.main{
  overflow: hidden;
  height: 97vh;
  display: flex;
  align-items: center;
  background-color: #2abeb2;
  justify-content: center;
}
.demo-ruleForm {
  align-items: center;
}
.text{
  font-size: 20px;
  font-weight: bold;
  margin-left: 300px;
  margin-top: 60px;
  margin-bottom: 50px;
}
.sign{
  display: flex;
  text-align: center;
  margin-left: 290px;
}

</style>
